Unit testing web services
Web services are similar
to any other method in the web application. We have to just add the
reference of the web service and call the method, as we call any other
method of the web application. All the methods and attributes required
for testing the web service are available under the namespace Microsoft.VisualStudio.TestTools.UnitTesting.Web.
Similar to the web
application, the web service can be hosted using IIS server or the local
development server. If it is hosted in the IIS, just add the reference
to the unit test application and call the web service method just like
calling the method in the web application. If it is available in the
local machine but not hosted in the web server, just add the attributes
required to start the development server. This attributes takes care of
hosting the service locally.
Let us create a simple web
service which returns values based on the input parameter value that is
passed to the web method. Given below is the sample code:
public class Service1 : System.Web.Services.WebService
unit testingweb services{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetuserDetails(int id)
{
string name = "";
if (id == 100)
name = "Subha";
if (id == 200)
name = "Satheesh";
return name;
}
}
After creating the web method, right-click on the method and select Create Unit Tests…. This will create the unit test with the attributes required for the web service test.
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\WebService1", "/")]
[UrlToTest("http://localhost:15558/")]
public void GetuserDetailsTest()
{
Service1 target = new Service1();
int id = 100;
string expected = "User Name";
string actual;
actual = target.GetuserDetails(id);
Assert.AreEqual(expected, actual);
}
The above unit test method
for the web method is very similar to the method for the ASP.NET
application. This web service runs in a local development server. If it
has to run under the IIS server, we can change the configuration
settings.
This is the property set for the Web Service project. The servers are by default set as Visual Studio Development Server. To change it to run on the IIS server, select the second option Use IIS web server and create virtual directory. Now the if we generate the unit test, the URL used for the unit test would be http://localhost/WebService1.
Just run the test similar to
the other test by changing the expected value and using the required
assert methods. Sometimes the test may fail because of unavailability of
the web server or the server may not be running. In that case we can
use the TryUrlRedirection method of the WebServiceHelper to try connecting to the URL before testing. The sample code for redirection would be
Assert.IsTrue(WebServiceHelper.TryUrlRedirection(target,
testContextInstance,
"MyServer"
),"Redirection failed."
);
Code coverage unit test
This is to see the methods or
the code has been covered by the unit test. This is the property we can
set on the project level before starting the unit testing. Open the
configuration file localtestrun.testrunconfig file under the solution. Select Code Coverage
from the list shown on the left side. Now you can see the list of
projects or artifacts to instrument options. Select the project for
which the code coverage has to be turned and select apply and close the
dialog.
Now select the test and run
the test again. Once the test is complete we can see the code coverage
details from the code coverage window which can be opened using the
option in the Test menu option.
The code coverage window shows the coverage details collected from the last test run. Below is the sample of the Code Coverage Result.
There was an error during the unit test of the BtnSubmit_ClickTest() method which is why the code coverage is only 50.00%. The other details are the percentage of the coverage of the code till the test failure.
If we know the fix for the
test to complete 100% coverage of the method, we can just right-click on
the particular method and select go to source code,
which will takes us to the source code of the particular method. We can
fix the code and rerun the test until the code is completely covered by
the test. To get more details on the code coverage, add additional
columns or remove existing columns to the output window.